package com.xiaoshuidi.cloud.module.report.xxljob;

import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.report.constant.ContractCompanyTypeEnum;
import com.xiaoshuidi.cloud.module.report.constant.RenterTypeEnum;
import com.xiaoshuidi.cloud.module.report.dal.DO.ContractRentorderDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.ContractRoomDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.FinanceMoneyFlowDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.RoomHouseDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.DepositDetailDO;
import com.xiaoshuidi.cloud.module.report.dal.mapper.ContractRentorderMapper;
import com.xiaoshuidi.cloud.module.report.dal.mapper.ContractRoomMapper;
import com.xiaoshuidi.cloud.module.report.dal.mapper.FinanceMoneyFlowMapper;
import com.xiaoshuidi.cloud.module.report.dal.mapper.RoomHouseMapper;
import com.xiaoshuidi.cloud.module.report.service.finance.DepositDetailService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 押金处理 job 每日10点 和18点跑一次
 */
@Component
@Slf4j
public class DepositDetailHandleJob {

    @Resource
    private DepositDetailService depositDetailService;
    @Resource
    private ContractRentorderMapper contractRentorderMapper;
    @Resource
    private ContractRoomMapper contractRoomMapper;
    @Resource
    private FinanceMoneyFlowMapper financeMoneyFlowMapper;
    @Resource
    private RoomHouseMapper roomHouseMapper;

    @XxlJob("depositDetailHandleJob")
    @TenantIgnore
    public void execute() {
        //获取未处理的押金明细数据
        List<DepositDetailDO> list = depositDetailService.selectListUnHandel();
        if(CollectionUtils.isEmpty(list)){
            log.info("没有需要处理的押金明细");
            return;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (DepositDetailDO e : list) {
            try {
                //查询账单
                ContractRentorderDO rentorderDO = contractRentorderMapper.selectById(e.getOrderId());
                if(rentorderDO==null||rentorderDO.getBusinessId()==null){
                    log.error("===脏数据此账单不应该为空 脏数据 orderId:{}",e.getOrderId());
                    continue;
                }
                //查询合同
                ContractRoomDO contractDO = contractRoomMapper.selectById(rentorderDO.getBusinessId());
                if(contractDO ==null){
                    log.error("====脏数据 有合同id没合同 contractId:{}",rentorderDO.getBusinessId());
                    continue;
                }
                FinanceMoneyFlowDO flowDO;
                if(rentorderDO.getTradeSerialNo() ==null){
                    log.error("====脏数据 有支付信息无支付数据 订单id  orderId:{}",rentorderDO.getId());
                    //TODO 临时处理
                    flowDO = new FinanceMoneyFlowDO();
                    flowDO.setRenter(contractDO.getCustomerName());
                    flowDO.setTrader(contractDO.getCustomerName());
                    flowDO.setPayMethod(rentorderDO.getPayMethod());
                    flowDO.setPayTime(rentorderDO.getActualPayTime());
//                    continue;
                }else {
                    //查询支付信息
                    flowDO = financeMoneyFlowMapper.selectByFlowNo(rentorderDO.getTradeSerialNo());
                    if(flowDO ==null){
                        log.error("====脏数据 有支付流水无支付数据  tradeSerialNo:{}",rentorderDO.getTradeSerialNo());
                        //TODO 临时处理
                        flowDO = new FinanceMoneyFlowDO();
                        flowDO.setRenter(contractDO.getCustomerName());
                        flowDO.setTrader(contractDO.getCustomerName());
                        flowDO.setPayMethod(rentorderDO.getPayMethod());
                        flowDO.setPayTime(rentorderDO.getActualPayTime());
//                        continue;
                    }
                }


                //查询房源信息
                RoomHouseDO houseDO = roomHouseMapper.selectById(rentorderDO.getRoomId());
                if(houseDO ==null){
                    log.error("====脏数据 有房间id无房源数据 roomId:{}",rentorderDO.getRoomId());
                    continue;
                }
                //拼凑需要数据
                e.setApartmentId(rentorderDO.getApartmentId());
                e.setRoomId(rentorderDO.getRoomId());
                e.setContractId(contractDO.getId());
                e.setContractNo(contractDO.getContractNo());
                String contractPeriod = contractDO.getStartTime().format(formatter) + "~" + contractDO.getEndTime().format(formatter);
                e.setContractPeriod(contractPeriod);
                e.setRenter(flowDO.getRenter());
                e.setTrader(flowDO.getTrader());
                e.setRenterType(RenterTypeEnum.getValueByType(contractDO.getCustomerType()));
                e.setPayMethod(flowDO.getPayMethod());
                e.setContractType(ContractCompanyTypeEnum.getValueByType(contractDO.getCustomerType()));
                e.setPayDate(flowDO.getPayTime());
                e.setHandleFlag(1);
                //集中式和分散式拼凑方法不一样
                StringBuilder houseName = new StringBuilder(houseDO.getArea()).append(houseDO.getBuildingNo()).append("栋");
                if (houseDO.getIsConcentrated()) {
                    //集中式 小区名-楼栋-房源名称   room_house.area - room_house.building_no栋 - room_house.name
                    e.setHouseResource(houseName.append(houseDO.getName()).toString());
                } else {
                    //分散式  小区名-楼栋-单元-房源名称 room_house.area - room_house.building_no栋 - room_house.unit_no单元 - room_house.name
                    e.setHouseResource(houseName.append(houseDO.getUnitNo()).append("单元").append(houseDO.getName()).toString());
                }
            } catch (Exception ex) {
                log.error("=====处理押金报表异常 contractId:{}",e.getContractId());
                ex.printStackTrace();
            }
        }
        if(!CollectionUtils.isEmpty(list)){
            depositDetailService.updateBatch(list);
        }
    }

}